package com.coffee.excel;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import org.apache.poi.ss.usermodel.*;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author: songkui
 * @since: 2023/2/14 11:18
 */
public class MochaExcelUtil {

    public static List<List<String>> head(List<String> header, String bigTitle) {
        List<String> head;
        List<List<String>> list = new LinkedList<>();
        for (String h : header) {
            head = new LinkedList<>();
            head.add(bigTitle);
            head.add(h);
            list.add(head);
        }
        return list;
    }

    public static List<List<String>> head(String[] header, String bigTitle) {
        List<String> head;
        List<List<String>> list = new LinkedList<>();
        for (String h : header) {
            head = new LinkedList<>();
            head.add(bigTitle);
            head.add(h);
            list.add(head);
        }
        return list;
    }

    public static String verifyRequiredField(Map<Integer, String> fieldMap, Map<Integer, String> data){
        Set<Integer> fieldSet = fieldMap.keySet();
        StringBuilder stringBuilder = new StringBuilder();
        for (Integer fieldColumn : fieldSet){
            if (StrUtil.isEmpty(data.get(fieldColumn))){
                stringBuilder.append(fieldMap.get(fieldColumn)).append(" 为必填项; \r\n");
            }
        }
        return stringBuilder.toString();
    }

    public static WriteCellStyle buildHeadDefaultStyle(){
        return buildHeadStyle(IndexedColors.WHITE.getIndex());
    }

    public static WriteCellStyle buildHeadStyle(Short colors){
        colors = null == colors || colors < 1 ? IndexedColors.WHITE.getIndex() : colors;
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 背景颜色
        headWriteCellStyle.setFillForegroundColor(colors);
        headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        // 字体
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontName("黑体");//设置字体名字
        headWriteFont.setFontHeightInPoints((short)14);//设置字体大小
        headWriteFont.setBold(true);//字体加粗
        headWriteCellStyle.setWriteFont(headWriteFont); //在样式用应用设置的字体;
        // 样式
        headWriteCellStyle.setBorderBottom(BorderStyle.MEDIUM);//设置底边框;
        headWriteCellStyle.setBottomBorderColor((short) 0);//设置底边框颜色;
        headWriteCellStyle.setBorderLeft(BorderStyle.MEDIUM);  //设置左边框;
        headWriteCellStyle.setLeftBorderColor((short) 0);//设置左边框颜色;
        headWriteCellStyle.setBorderRight(BorderStyle.MEDIUM);//设置右边框;
        headWriteCellStyle.setRightBorderColor((short) 0);//设置右边框颜色;
        headWriteCellStyle.setBorderTop(BorderStyle.MEDIUM);//设置顶边框;
        headWriteCellStyle.setTopBorderColor((short) 0); //设置顶边框颜色;
        headWriteCellStyle.setWrapped(true);  //设置自动换行;
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);//设置水平对齐的样式为居中对齐;
        headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);  //设置垂直对齐的样式为居中对齐;

        //        headWriteCellStyle.setShrinkToFit(true);//设置文本收缩至合适

        return headWriteCellStyle;
    }

    public static CellStyle buildResultStyle(Workbook workbook, boolean isSuccess){
        Short color = isSuccess ? IndexedColors.BLACK1.getIndex() : IndexedColors.RED.getIndex();
        return buildCellStyle(workbook, color);
    }

    public static CellStyle buildCellStyle(Workbook workbook, Short color){
        CellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setColor(color); //(IndexedColors.WHITE.getIndex()); //红色
        font.setBold(true); //是否加粗
        style.setFont(font);
        style.setWrapText(true);
        return style;
    }


}
